<?php
/**
 * Index.class.php 首页接口
 *
 * @author  周剑锋 <camee@foxmail.com>
 * @since   1.0.0 2017-10-24
 */

namespace controls\MobileApi\Home;

use Center\Active;
use Center\Ads;
use Center\City;
use Center\SiteData;
use Mobile\RecomData;
use MobileApi\ApiBase;
use Notice\Sys;

/**
 * 首页接口
 *
 * @package Home
 * @author  周剑锋 <camfee@foxmail.com>
 * @since   1.0.4 2017-10-24
 */
class Index extends ApiBase
{

    /**
     * 获取开通城市
     *
     * <pre>
     * GET:
     *      无传入参数
     * </pre>
     *
     * @return void|String
     *
     * <pre>
     * {
     *     "Status": 200,
     *     "Result": {
     *         "CityList": [
     *             {
     *                 "CityId": 11,      // 城市id
     *                 "CityName": "醴陵" // 城市名称
     *             },...
     *         ]
     *     },
     *     "ServerTime": 1514898568
     * }
     * </pre>
     */
    public function getValidCityList()
    {
        $list = City::getAllCity();
        $data['CityList'] = [];
        if (!empty($list['data'])) {
            foreach ($list['data'] as $v) {
                $data['CityList'][] = [
                    'CityId' => $v['CityId'],
                    'CityName' => $v['CityName'],
                ];
            }
        }

        $this->output(200, $data);
    }

    /**
     * 商家有礼首页接口
     *
     * <pre>
     * GET:
     *     city:   必选，城市名称
     *     offset: 可选， 偏移量 默认0
     *     limit:  可选， 每次数量, 最高不超过50 默认20
     * </pre>
     *
     * @return void|String
     *
     * <pre>
     * {
     *     "Status": 200,
     *     "Result": {
     *         "OnlineNum": 1344, // 在线人数
     *         "RedDot": 1,  // 是否有新消息 1：是 0：否
     *         "CityId": 11,    // 城市id
     *         "CityName": "醴陵", // 城市名称
     *         "RewardList": [ // 获奖列表
     *             {
     *                 "UserNick": "用户名",
     *                 "Text": "获得商家赠送的商品名"
     *             },...
     *         ],
     *         "BigBanner": { // 置顶大图活动
     *             "ActiveId": 1, // 活动id
     *             "ActiveName": "活动标题动", // 活动名称
     *             "ActiveCover": "图片地址", // 活动封面图
     *             "RemainTime": 222,  // 活动剩余时间（s）
     *             "ViewCount": 222  // 浏览数
     *         },
     *         "AdviseList": [ // 广告图片列表
     *             {
     *                 "ImgUrl": "广告图片地址",
     *                 "Title": "广告标题"
     *             },...
     *         ],
     *         "Total": 15, // 活动总数
     *         "List": [ // 活动列表
     *             {
     *                 "ActiveId": 1,
     *                 "ActiveName": "活动标题最多两行超过打点活动",
     *                 "ActiveCover": "图片地址",
     *                 "StartTime": '2018-01-17 00:19:23' // 活动开始时间
     *                 "RemainTime": 1516558978,
     *                 "ViewCount": 222
     *             },...
     *         ]
     *     },
     *     "ServerTime": 1511095224
     * }
     * 异常情况
     * 201：参数错误
     * </pre>
     */
    public function getIndex()
    {
        $city = trim($_GET['city']);
        $city_id = (int)City::getCityInfoByName($city)['Division'];
        if ($city_id < 1) {
            $this->output(201, '参数错误');
        }
        $offset = (int)$_GET['offset'];
        $limit = (int)$_GET['limit'];
        $offset = max(0, $offset);
        $limit = $limit < 1 ? 20 : min($limit, 50);

        // 首页大图活动
        $data = RecomData::getData(RecomData::APP_ACTIVE_BANNER)[RecomData::APP_ACTIVE_BANNER];
        $big = [];
        $active_id = 0;
        if (!empty($data[$city_id])) {
            $active_id = $data[$city_id];
            $big = Active::getInfoByIds($active_id);
        }

        $list_info = Active::getListByCity($city_id, $offset, $limit);
        $list = [];
        if (!empty($list_info['data'])) {
            foreach ($list_info['data'] as $v) {
                if ($v['Id'] != $active_id) {
                    $list[] = [
                        'ActiveId' => (int)$v['Id'],
                        'ActiveName' => $v['Name'],
                        'ActiveCover' => $v['Cover'],
                        'ShopId' => (int)$v['ShopId'],
                        'StartTime' => $v['StartTime'],
                        'RemainTime' => strtotime($v['EndTime']),
                        'ViewCount' => (int)$v['ViewCount'],
                    ];
                }
            }
        }
        //红点
        $reddot = 0;
        $uid = $this->isLogin(false);
        if ($uid > 0) {
            $rednum = Sys::getNum($uid);
            $reddot = $rednum[Sys::REDIS_NUM_FIELD_SHOP_MSG] > 0 ? 1 : 0;
        }


        $ads = Ads::getAdsByCityId(1);
        $AdviseList = [];
        if (count($ads['data']) > 0) {
            foreach ($ads['data'] as $adv) {
                $AdviseList[] = [
                    'ImgUrl' => $adv['Cover'],
                    'Title' => $adv['Title'],
                ];
            }
        }
        $livenum = SiteData::getDataByKey('LiveNumber');
        $reward = SiteData::getDataByKey('RewardList');
        $rewardlist = unserialize($reward);
        $rewardinfo = [];
        foreach ($rewardlist as $rv) {
            if ($rv['UserNick']) {
                $rewardinfo[] = $rv;
            }
        }
        $data = [
            'OnlineNum' => $livenum + rand(1, 88), // TODO 在线人数
            'RedDot' => $reddot, // TODO 消息
            'CityId' => $city_id,
            'CityName' => $city,
            'RewardList' => $rewardinfo,
            'AdviseList' => $AdviseList,
            'Total' => (int)$list_info['count'],
            'List' => $list
        ];
        if (!empty($big)) {
            $data['BigBanner'] = [
                'ActiveId' => (int)$big['Id'],
                'ActiveName' => $big['Name'],
                'ActiveCover' => $big['Cover'],
                'ShopId' => (int)$big['ShopId'],
                'StartTime' => $big['StartTime'],
                'RemainTime' => strtotime($big['EndTime']),
                'ViewCount' => (int)$big['ViewCount'],
            ];
        }

        $this->output(200, $data);
    }
}